<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of c_wockplace_controller
 *
 * @author tonatos
 */
class c_workplace_controller extends c_base_controller {

    public $module = "workplace";
    public $login_page = 691;
    public $user_id;

    public function __construct() {
        parent::__construct();

        global $modx;

        if (!$modx->getLoginUserID()) {
            $_SESSION['mvc_login_callback'] = $_SERVER['REQUEST_URI'];
            $modx->sendRedirect($modx->makeUrl($this->login_page));
        } else {
            $this->user_id = $modx->getLoginUserID();
        }
    }

    public function index() {
        $this->add_modx_menu("Личный кабинет");
        $this->set_modx_field("pagetitle", "Личный кабинет");
        $this->user = new c_web_users_model($this->user_id);
        $this->show("index");
    }

    public function change_password() {

        $this->set_modx_field("pagetitle", "Сменить пароль");

        $this->add_modx_menu("Личный кабинет", "/cabinet.html");
        $this->add_modx_menu("Cменить пароль");


        $form = new c_form_controller();
        $form->add_varchar("password", "Введите новый пароль", "Внимание символы отображаются", 1)
                ->set_min(6);

        $form->submit_text = "Сохранить";
        $form->add_cancel("/cabinet.html");

        if ($form->data_submitted()) {
            if ($form->is_validate()) {

                $data = $form->get_data();
                $user = new c_web_users_model($this->user_id);
                $user->password = md5($data['password']);
                $user->cachepwd = "";
                $user->save();
                echo "<div class='message_ok'>Новый пароль успешно установлен</div>";
            }
        }

        $form->show_form();
    }

    public function profile() {
        $this->set_modx_field("pagetitle", "Профайл");

        $this->add_modx_menu("Личный кабинет", "/cabinet.html");
        $this->add_modx_menu("Профайл");

        $form = new c_form_controller("profile_form");

        $user = new c_web_users_model($this->user_id);

        $form->add_html("<div class='field'>Логин: $user->username</div><div class='grid_5'>");

        $form->add_varchar("fullname", "Фамилия И.О.", "", 1);
        $form->add_email("email", "Email", "", 1)
                ->ignore = $user->email;

        $form->add_date("dob", "День рождения", "Формат ДД.ММ.ГГГГ");


        $form->add_html("</div><div class='grid_1'>&nbsp;</div><div class='grid_5'>");

        $form->add_photoimage("photo", "Фотография");

        $form->add_radio("gender", "Пол", "", 0)
                ->set_options(array(1 => "Муж", 2 => "Жен"));

        $form->add_html("</div>");
        $form->add_cancel("/cabinet.html");
        if ($form->data_submitted()) {
            if ($form->is_validate()) {

                $data = $form->get_data();

                foreach ($data as $key => $value) {
                    $user->$key = $value;
                }
                $user->save();

                echo "<div class='message_ok'>Данные успешно сохранены</div>";
            }
        } else {
            //по умолчанию
            $form->set_data($user);
        }

        $form->show_form();
    }


    public function do_ajax_fileupload($params) {
        global $modx;
        
        $form = new c_form_controller();

        $result = $form->do_photoimage_upload($params[2]);
        if ($result===false){
            return "{error: '$form->ajax_upload_error',msg: ''}";
        }

        $user_id = $modx->getLoginUserId();

        $thumb = str_replace("webusers/$user_id/","webusers/$user_id/tn_",$result);

        return "{error: '',msg: '$result',thumb:'$thumb'}";
    }

    public function subscribe(){
        global $modx;

        $this->set_modx_field("pagetitle", "Управление подпиской");

        $this->add_modx_menu("Личный кабинет", "/cabinet.html");
        $this->add_modx_menu("Управление подпиской");

        $this->form = new c_form_controller();
        $this->form->add_multi_checkbox("subscribes","")->set_options(array("site"=>"Подписаться на весь сайт","forum"=>"Подписаться на форум"));

        $user = new c_web_users_model($this->user_id);
        if ($this->form->data_submitted()) {
            if ($this->form->is_validate()) {
                $data =$this->form->get_data();
                $user->subscribes = implode(",",$data['subscribes']);
                $user->save();

                echo "<div class='message_ok'>Данные успешно сохранены</div>";
            }
        }else{
            $this->form->get_field("subscribes")->set_value(explode(",",$user->subscribes));
        }


        $this->form->show_form();

        //$this->show("subscribe");
    }
}

?>
